app.controller('DriverModalCtrl', function ($scope, $modalInstance, $modal, toaster, param, ajax, fileUpload, cos) {

	$scope.submitting = false;
	$scope.param = param;
	if ($scope.param.data == null) $scope.param.data = {};
	if ($scope.param.type == "update") $scope.flag = true;

	//获取组织列表
	$scope.loadOrgList = function () {
		ajax.call(new Req.GET({
			resource: 'organization/list'
		})).then(function (result) {
			$scope.orglist = result.result;
			//设置默认值
			if ($scope.param.data.org != null) {
				var id = $scope.param.data.org.id;
				var sel = 0;
				for (var i = 0; i < $scope.orglist.length; i++) {
					sel++;
					if ($scope.orglist[i].id == id) {
						break;
					}
				}

				$scope.orglist.selected = $scope.orglist[sel - 1];
			} else {
				$scope.orglist.selected = $scope.orglist[0];
			}

		}, function () { });
	};
	$scope.loadOrgList();

	$scope.ok = function () {
		var uploadUrl = cos.host() + cos.img() + "?attributeName=driver";
		if ($scope.param.type == "insert") {
			if ($scope.myForm.$valid) {
				if ($scope.password == undefined || $scope.password == "" || $scope.password.length < 6) {
					toaster.pop('warning', '错误', '密码长度必须大于等于6');
					$scope.submitting = false;
					return;
				} else if ($scope.newpassword == undefined || $scope.newpassword == "" || $scope.newpassword.length < 6) {
					toaster.pop('warning', '错误', '确认密码长度必须大于等于6');
					$scope.submitting = false;
					return;
				} else if ($scope.password != $scope.newpassword) {
					toaster.pop('warning', '错误', '两次密码输入不一致');
					$scope.submitting = false;
					return;
				}
				if ($scope.fileToUpload != undefined) {
					fileUpload.upload($scope.fileToUpload, uploadUrl, null,
						function (result) {
							if (result.code == 200) {
								$scope.param.data.licensePhoto = result.filePath;

							} else {
								toaster.pop('error', '错误', result.message);
							}
							$scope.uploading = false;
						}, function () {
							toaster.pop('error', '错误', '文件上传失败！');
							$scope.uploading = false;
						}
					);
				}
				$scope.param.data.password = $scope.password;
				$scope.param.data.org = $scope.orglist.selected;
				$modalInstance.close($scope.param);
			}
		} else if ($scope.param.type == "update") {
			if ($scope.myForm.$valid) {
				if ($scope.password != undefined) {
					if ($scope.password.length >= 6) {
						$scope.param.data.password = $scope.password;
						$modalInstance.close($scope.param);
					} else if ($scope.password.length < 6 && $scope.password.length > 0) {
						toaster.pop('warning', '错误', '密码长度必须大于等于6');
						$scope.submitting = false;
						return;
					}
				}
				if ($scope.fileToUpload == undefined) {
					$scope.param.data.org = $scope.orglist.selected;
					$modalInstance.close($scope.param);
				} else {
					fileUpload.upload($scope.fileToUpload, uploadUrl, null,
						function (result) {
							if (result.code == 200) {
								$scope.param.data.licensePhoto = result.filePath;
								$scope.param.data.org = $scope.orglist.selected;
								$modalInstance.close($scope.param);
							} else {
								toaster.pop('error', '错误', result.message);
							}
							$scope.uploading = false;
						}, function () {
							toaster.pop('error', '错误', '文件上传失败！');
							$scope.uploading = false;
						}
					);
				}
			}
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
		$scope.submitting = false;
	};
});